import requests
import datetime

base_url = 'https://ncportal.esrichina.com.cn/JKZX/yq_{}.json'
# 设置开始时间
str_time = '20200116'
# 设置结束时间
str_endtime = '20200227'
num = 0
while str_time <= str_endtime:
    str_time = datetime.datetime.strptime(str_time, '%Y%m%d')
    
    utc_time = str_time + datetime.timedelta(hours=+num)
    str_time = datetime.datetime.strftime(utc_time, "%Y%m%d")
    num += 1
    url = base_url.format(str_time)
    response = requests.get(url)
    print(response.text)
    result = response.json()
    features = result['features']
    filename = str_time + '.csv'
    with open(filename, 'a') as f:
        content = '省份' + ',' + '新增疑似' + ',' + '累计疑似' + ','+ '新增确诊' + ',' + '累计确诊' + ','+ '新增死亡' + ','+ '累计死亡' +  '\n'
        f.write(content)
        for feature in features:
            sf = feature['properties']['省份']
            xzys = feature['properties']['新增疑似']
            ljys = feature['properties']['累计疑似']
            xzqz = feature['properties']['新增确诊']
            ljqz = feature['properties']['累计确诊']
            xzsw = feature['properties']['新增死亡']
            ljsw = feature['properties']['累计死亡']
            content = str(sf) + ',' + str(xzys) + ',' + str(ljys) + ','+ str(xzqz) + ','+ str(ljqz) + ','+ str(xzsw) + ',' + str(ljsw) + '\n'
            f.write(content)

# with open('yq.json', 'wb') as f:
#     f.write(response.content)